home *** CD-ROM | disk | FTP | other *** search
/ Night Owl 6 / Night Owl's Shareware - PDSI-006 - Night Owl Corp (1990).iso / 039a / cppcom.zip / CHARQUEU.CPP < prev    next >
C/C++ Source or Header  |  1991-04-14  |  3KB  |  111 lines

  1. /***************************************************************************
  2. These C++ classes are copyright 1990, by William Herrera.
  3. All those who put this code or its derivatives in a commercial product MUST
  4. mention this copyright in their documentation for users of the products in
  5. which this code or its derivative classes are used.  Otherwise, this code
  6. may be freely distributed and freely used for any purpose.
  7.  
  8. Enhancements: 1991 by David Orme
  9.     *  General cleanup.
  10.             - I/O now takes advantage of C++ overloading.
  11.             - Serial port I/O functionality now only in Serial class.
  12.             - Modem functionality now only in Modem class.
  13.     *  Possible to easily implement file Xfr prots now.
  14.     *  CCITT CRC-16 class added                            -- 2-20-1991
  15.     *  BIOS Timer class added                                -- 2-22-1991
  16.     *  Optional timeout on all input routines added    -- 2-25-1991
  17.  
  18. ***************************************************************************/
  19.  
  20. // file charqueu.cpp class definitions for CharQueue class.
  21.  
  22. #include <stdio.h>
  23. #include <stdlib.h>
  24.  
  25. #include "CharQueu.hpp"
  26.  
  27. static const char mem_out_err[] = "CharQueue error:  Out of memory";
  28.  
  29. CharQueue::CharQueue(unsigned int bufsize) 
  30. {
  31.     size = bufsize;
  32.     count = 0;
  33.     queue_buffer = new char[size];
  34.     if(queue_buffer == NULL)
  35.     {
  36.         fputs(mem_out_err, stderr);
  37.         exit(-1);
  38.     }
  39.     add_position = 0;
  40.     get_position = 0;    // get_position chases add_position
  41. }
  42.  
  43. CharQueue::~CharQueue() 
  44.     delete queue_buffer; 
  45. }
  46.  
  47. void CharQueue::Purge() 
  48.     count = 0; 
  49.     add_position = 0; 
  50.     get_position = 0; 
  51. }
  52.  
  53. unsigned int CharQueue::GetCount() 
  54.     return count; 
  55. }
  56.  
  57. boolean CharQueue::IsFull()    
  58.     return (count == size) ? true : false; 
  59. }
  60.  
  61. boolean CharQueue::IsEmpty() 
  62.     return (count == 0) ? true : false; 
  63. }
  64.  
  65. int CharQueue::Add(char ch) 
  66. {
  67.     if(count == size)
  68.     {
  69.         fputs(mem_out_err, stderr);
  70.         return 1;                    // return 1 for error
  71.     }
  72.     else
  73.     {
  74.         ++count;
  75.         queue_buffer[add_position] = ch;
  76.         ++add_position;
  77.         if(add_position == size)        // end of queue memory
  78.             add_position = 0;        // so wrap to bottom 
  79.         return 0;                    // return 0 for success
  80.     }
  81. }
  82.  
  83. int CharQueue::Get()
  84. {
  85.     // remove and return next char in line.
  86.     int ch = -1;                    // -1 return for empty queue
  87.     if(count > 0)
  88.     {
  89.         count--;
  90.         ch = queue_buffer[get_position] & 0xFF;
  91.         ++get_position;
  92.         if(get_position == size)
  93.             get_position = 0;        // wrap to bottom
  94.     }
  95.     return ch;
  96. }
  97.  
  98. int CharQueue::Peek()
  99. {
  100.     // return current character but don't remove from buffer.
  101.     return (count > 0) ? queue_buffer[get_position] & 0xFF : -1;
  102. }
  103.  
  104.  
  105. // end of file CharQueu.cpp
  106.